package org.NooLab.itexx.app.gui;

import java.io.IOException;
import java.util.ArrayList;

import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.app.controller.ItexxModelController;
import org.NooLab.itexx.app.gui.handlers.PanelText;





public class StackedPanels {

	ITexxWindowCommons appCommons;
	ArrayList<StackedPanel> items = new ArrayList<StackedPanel>();
	
	// ========================================================================
	public StackedPanels(ITexxWindowCommons appCommons){
		this.appCommons = appCommons;
	}
	// ========================================================================
	
	public ArrayList<StackedPanel> getItems(){
		return items;
	}
	
	
	public StackedPanel getItem(int index){
		StackedPanel  item=null;
		
		if ((index>=0) && (index<items.size())){
			item = items.get(index) ;
		}
		
		return item;
	}
	
	
	public int size(){
		return items.size() ;
	}
	
	public void add(StackedPanel item){
		items.add(item) ;
	}

	public void insert( int position, ArrayList<StackedPanel> _items){
		
		int p = position ;
		StackedPanel item;
		
		
		for (int i=0;i<_items.size();i++){
			item = _items.get(i);
			items.add(p+i,item) ;
		}
	}

	public StackedPanel insert( int position, StackedPanel item) throws IOException{
		StackedPanel sp, spp=null;
		
		
		
		// physically, the item has already been added just due to create() !!!
		// hence we only need to add it to the logical list
		items.add( item ) ; 
		
		String guid = item.guidStr;
		PanelText ptxt = item.getPanelText() ;
		// CombiPanel cp = item.combiPanel;
		
		// moving content
		for (int i=items.size()-1;i>position;i--){
			// relocate text from (i-2) to (i-1)
			moveContent(i-1,i);
		}
		
		// setting content for item at position to that provided by the imported container
		// note, that all the text stuff is in a dedicated object
		if (position>=items.size()){
			position--;
		}
		if (position>=0){
			spp = items.get(position);
			
			spp.guidStr = guid;
			spp.setPanelText( ptxt );
		}
		
		
		// spp.combiPanel = cp;
		
		// returning the item at position 
	
		return spp;
	}

	public void remove(int index) {
		// 
		StackedPanel p = items.get(index); 
		
		p.combiPanel.setVisible(false);
		p.combiPanel.paneltext.clearText();
		
		
		ItexxModelController mc = p.panelProducer.appCommons.getModelController();
		
		p=null;
		items.remove(index) ;
		
	}

	private void copyContent(StackedPanel fromItem, StackedPanel toItem) {
		// 
		
		String guid ;
		PanelText content ;
		
		guid = fromItem.guidStr;
		content = fromItem.getPanelText() ;
		
		toItem.guidStr = guid;
		toItem.setPanelText( content );
		 
		toItem.strgutil = fromItem.strgutil ;
		toItem.combiPanel = fromItem.combiPanel ;
		
		toItem.combiPanel.updateText();
	}
	
	private void moveContent(StackedPanel fromItem, StackedPanel toItem) {

		PanelText panelText ;
		
		copyContent(fromItem, toItem);
				
		fromItem.guidStr = "";
		panelText = fromItem.getPanelText();
		fromItem.setPanelText("");
		
		toItem.combiPanel.updateText();
		fromItem.combiPanel.updateText();
		
	}
	
	private void moveContent(int fromItemPos, int toItemPos) {
		 
		String guid ;
		PanelText content ;
		
		guid = items.get(fromItemPos).guidStr;
		content = items.get(fromItemPos).getPanelText() ;
		
		items.get(toItemPos).guidStr = guid;
		items.get(toItemPos).setPanelText( new PanelText(content) );
		
		items.get(fromItemPos).guidStr = "";
		items.get(fromItemPos).setPanelText( "" );
		
		// items.get(toItemPos).combiPanel.setParent( items.get(fromItemPos) );
		// items.get(fromItemPos).combiPanel = null;
		
		items.get(toItemPos).combiPanel.updateText();
		
	}
	

	
}
